<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
class MY_Controller extends CI_Controller
{
	protected $sys;
	protected $user;
	protected $l;
	protected $class_list;
	protected $class_page;
	protected $cfg;
	protected $item_status;

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');
		$this->load->library('pagination');
		$this->load->library('pwd');
		$this->load->library('d');
		$this->load->helper('url');
		$this->load->helper('num');
		$this->load->helper('chk');
		$this->load->helper('language');
		$this->load->helper('captcha');
		$this->load->helper('ajax_return');
		$this->load->helper('cookie');
		$this->load->helper('tool');
		date_default_timezone_set('PRC');
		$lang = $this->input->get('lang');

		if ($lang) {
			if (!in_array($lang, array('cn', 'en'))) {
				$lang = 'cn';
			}

			set_cookie('lang', $lang);
		}
		else {
			$lang = get_cookie('lang');
		}

		if (!in_array($lang, array('cn', 'en'))) {
			$lang = 'cn';
		}

		$this->lang->load(array('article', 'error', 'home', 'nav', 'orders', 'record', 'sign', 'trade', 'user', 'footer', 'money', 'header'), $lang);
		$this->l = $lang;
		$this->user = $this->d->find(array('table' => 'user', 'where' => 'id=' . $this->session->user_id));
		$this->sys = $this->d->find(array('table' => 'system', 'where' => 'id=1'));
		$this->cfg = $this->d->find(array('table' => 'money_config', 'where' => 'id=1'));
		$this->item_status = array('未提交', '待审核', '进行中', '已成功', '即将开始', '失败', '未通过');
	}

	protected function show($tpl, $d = array())
	{
		$this->class_list = $this->d->rows(array('table' => 'article_class', 'where' => 'mod="list"'));
		$this->class_page = $this->d->rows(array('table' => 'article_class', 'where' => 'mod="page"'));

		if ($this->l == 'en') {
			$this->sys['name'] = $this->sys['en_name'];
			$this->sys['title'] = $this->sys['en_title'];
			$this->sys['keyword'] = $this->sys['en_keyword'];
			$this->sys['description'] = $this->sys['en_description'];
			$this->sys['address'] = $this->sys['en_address'];
			$this->sys['intro'] = $this->sys['en_intro'];

			foreach ($this->class_list as $k => $v) {
				$this->class_list[$k]['name'] = $v['en_name'];
			}

			foreach ($this->class_page as $k => $v) {
				$this->class_page[$k]['name'] = $v['en_name'];
			}
		}

		$d['ctl'] = $this->uri->segment(1);
		$d['fn'] = $this->uri->segment(2);
		$d['sys'] = $this->sys;
		$d['cfg'] = $this->cfg;
		$d['lang'] = $this->l;
		$d['user'] = $this->user;
		$d['class_list'] = $this->class_list;
		$d['class_page'] = $this->class_page;
		$d['frend'] = $this->d->rows(array('table' => 'frend', 'where' => 'status=1', 'order' => 'sort asc'));
		$d['notice'] = $this->d->find(array('table' => 'article', 'where' => 'cid=1'));
		$d['notice']['class_name'] = '网站公告';
		$d['t'] = rand(100, 999);

		if (is_array($tpl)) {
			foreach ($tpl as $v) {
				if ($v) {
					$this->load->view($v, $d);
				}
			}
		}
		else {
			$this->load->view($tpl, $d);
		}
	}

	protected function verify_user()
	{
		if (!isset($this->session->user_id) || (intval($this->session->user_id) <= 0)) {
			redirect('user/login');
		}
	}

	protected function verify_user_ajax()
	{
		if (!isset($this->session->user_id) || (intval($this->session->user_id) <= 0)) {
			ajax_return(1, '请先登录');
		}
	}

	protected function verify_real_ajax()
	{
		if (!$this->user['idno'] || !$this->user['name']) {
			ajax_return(1, '请先实名认证');
		}
	}

	protected function verify_ajax()
	{
		if (!$this->user['mobile'] || !$this->user['idno'] || !$this->user['name'] || !$this->user['pic_front'] || !$this->user['pic_back'] || !$this->user['pic_hand']) {
			ajax_return(1, '请先完善您的认证资料');
		}
	}

	protected function verify_redirect()
	{
		if (!$this->user['mobile'] || !$this->user['idno'] || !$this->user['name'] || !$this->user['pic_front'] || !$this->user['pic_back'] || !$this->user['pic_hand']) {
			redirect('safe');
		}
	}

	protected function verify_url()
	{
		if (isset($_SERVER['HTTP_REFERER'])) {
			$from = parse_url($_SERVER['HTTP_REFERER']);
			$domain = domain($this->config->item('base_url'));

			if (!in_array($from['host'], array($domain, 'www.' . $domain))) {
				redirect('/error404.html');
			}
		}
		else {
			redirect('/error404.html');
		}
	}

	protected function verify_url_ajax()
	{
		if (isset($_SERVER['HTTP_REFERER'])) {
			$from = parse_url($_SERVER['HTTP_REFERER']);
			$domain = domain($this->config->item('base_url'));

			if (!in_array($from['host'], array($domain, 'www.' . $domain))) {
				ajax_return(1, '没有权限');
			}
		}
		else {
			ajax_return(1, '没有权限');
		}
	}

	protected function result_list($a)
	{
		$table = $a['table'];
		$where = $a['where'];
		$url = $a['url'];
		$order = $a['order'];
		$per_page = $a['per_page'];

		if (!$where) {
			$where = '1=1';
		}

		$config['base_url'] = site_url($url);
		$config['total_rows'] = $this->db->where($where)->count_all_results($table);
		$config['per_page'] = $per_page;
		$config['uri_segment'] = 3;
		$config['num_links'] = 2;
		$config['first_link'] = '首页';
		$config['first_link'] = '尾页';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config);
		$page = intval($this->uri->segment(3));
		$d['page'] = $this->pagination->create_links();
		$d['list'] = $this->db->where($where)->limit($config['per_page'], $page)->order_by($order)->get($table)->result_array();
		return $d;
	}

	protected function send_email($email, $captcha)
	{
		$this->load->library('mail');
		$subject = '验证您的邮箱---' . $this->sys['name'] . '';
		$body = '<p>尊敬的' . $this->sys['name'] . "用户，您好:</p>\r\n\t\t\t\t<p>您在 " . date('Y-m-d H:i:s', time()) . " 提交了验证邮箱请求，请使用以下</p>   \r\n\t\t\t\t<p style=\"color:red;\">验证码：<b>" . $captcha . "</b></p>\r\n\t\t\t\t<p>您若未提交此请求，您可以安全的忽略此邮件。可能有人错误的输入了您的邮件地址。</p>\r\n\t\t\t\t<p>感谢您的支持 ! " . $this->sys['name'] . '服务团队</p>';
		$smtpserver = 'smtpdm-ap-southeast-1.aliyun.com';
		$smtpserverport = 25;
		$smtpusermail = $this->sys['email_username'];
		$smtpemailto = $email;
		$smtpuser = $this->sys['email_username'];
		$smtppass = $this->sys['email_password'];
		$mailsubject = $subject;
		$mailsubject = '=?UTF-8?B?' . base64_encode($mailsubject) . '?=';
		$mailbody = $body;
		$mailtype = 'HTML';
		$this->mail->smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
		$this->mail->debug = false;
		$this->mail->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
	}

	protected function get_address($host, $username, $password, $port, $by)
	{
		require_once $_SERVER['DOCUMENT_ROOT'] . '/../Bitcoin/bitcoin.inc';
		$btc = new BitcoinClient('http', $username, $password, $host, $port);
		return $btc->getnewaddress($by);
	}

	protected function get_ethaddress($host, $port, $by)
	{
		$data = array(
			'jsonrpc' => '2.0',
			'method'  => 'personal_newAccount',
			'params'  => array($by),
			'id'      => 1
			);
		$data_string = json_encode($data);
		$ch = curl_init('http://' . $host . ':' . $port);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
		$result = curl_exec($ch);
		$arr = json_decode($result, true);
		return $arr['result'];
	}
}

?>
